package com.dsp.web;

import java.math.BigDecimal;
import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.CompanyAccount;
import com.dsp.bean.Message;
import com.dsp.services.CompanyAccountService;

@Controller("CompanyAccountController")
public class CompanyAccountController extends DspController<CompanyAccount> {

	public static String[] QueryColumn={"province","city","area","hukou","itemId","companyType"};
	public static String[] QueryType = {"String","String","String","Integer","Integer","Integer"};
	public CompanyAccountController() {
		super(CompanyAccount.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private CompanyAccountService companyAccountService;

	@RequestMapping(value = "/company_accounts", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("company_account/list");
		mav.addObject("path", "company_account/list.jsp");
		mav.addObject("current", "company_accounts");
		return mav;
	}

	@RequestMapping(value = "/company_accounts_list")
	@ResponseBody
	public Pagination<CompanyAccount> index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(CompanyAccount.class);
		super.getPageParameters(dc, QueryColumn	, QueryType);
		Pagination<CompanyAccount> p = this.companyAccountService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/company_accounts_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody CompanyAccount companyAccount) {
		Message message = new Message();
		logger.info("正在创建用户信息");
		try {
			if(companyAccount.getCompanyLimit() == null){
				 companyAccount.setCompanyLimit(new BigDecimal(99999));
			}
			if(companyAccount.getCompanyLower() == null){
				 companyAccount.setCompanyLower(new BigDecimal(0));
			}
			if(companyAccount.getCompanyType() == 2){ //定值
				companyAccount.setRate(null); //清除百分比
			} else {
				companyAccount.setFixValue(null);//清除定值
			}
			if (companyAccount.getId() != null) {
				this.companyAccountService.update(companyAccount);
			} else {
				this.companyAccountService.save(companyAccount);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		logger.info("完成创建信息");
		return message;
	}

	@RequestMapping(value = "/company_accounts_edit", method = RequestMethod.POST)
	@ResponseBody
	public CompanyAccount edit(@RequestBody CompanyAccount _companyAccount) {
		CompanyAccount companyAccount = this.companyAccountService.findById(_companyAccount.getId());
		return companyAccount;
	}

	@RequestMapping(value = "/company_accounts_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody CompanyAccount companyAccount) {
		Message message = new Message();
		String succ = this.companyAccountService.delete(companyAccount);
		message.setSuccess(Boolean.valueOf(succ));
		return message;
	}

	/**
	 * @return the companyAccountService
	 */
	public CompanyAccountService getCompanyAccountService() {
		return companyAccountService;
	}

	/**
	 * @param companyAccountService
	 *            the companyAccountService to set
	 */
	public void setCompanyAccountService(CompanyAccountService companyAccountService) {
		this.companyAccountService = companyAccountService;
	}

}
